package whu.cn.whushare.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
 * 类 名：用户类User
 * 功 能：定义User的全局域，设置其可呗Google gson转化为json数据
 *       定义User内部成员变量的设置和获取方法：setXXX() & getXXX()
 *       定义内部类Builder，用于创建一个User对象
 *       实现Parcelable接口中Creator<T>接口方法：createFromParcel(Parcel source) 和 newArray(int size)
 */
public class User implements Parcelable {
    //定义全局用户信息
	public static final String USER_ID = "userId";
	public static final String NICK_NAME = "nickname";
    public static final String EMAIL = "email";
	public static final String TOKEN = "token";
	public static final String AVATAR_INDEX = "avatarIndex";
	public static final String SNS_NICK_NAME = "snsNickname";
	public static final String IS_CURRENT_LOGIN_ACCOUNT = "isCurrentLoginAccount";

	public static final short IS_CURRENT_LOGIN_ACCOUNT_TRUE = 1;
	public static final short IS_CURRENT_LOGIN_ACCOUNT_FALSE = 0;

	@Expose                     //指明下列字段可以被Google gson所获取
	@SerializedName("userId")   //id可以被Google gson序列化为json数据，数据名为userid
	private String id;
	@Expose
	@SerializedName("photo")
	private String avatar;
	@Expose
	@SerializedName("email")
	private String email;
	@Expose
	@SerializedName("introduce")
	private String introduce;
	@Expose
	@SerializedName("autograph")
	private String brief;
	@Expose
	private String firstName;
	@Expose
	private String lastName;
	@Expose
	@SerializedName("createTime")
	private String createTime;
	@Expose
	@SerializedName("gender")
	private String gender;
	@Expose
	private String password;
	@Expose
	private String company;
	@Expose
	private String department;
	@Expose
    @SerializedName("position")
	private String title;
	@Expose
	private String idCode;
	@Expose
	private String workExperience;

    @Expose
    private String token;

	private short isCurrentLoginAccount;

	private int permission;
    @Expose
    @SerializedName("nickName")
	private String nickName;
    @Expose
    @SerializedName("phoneNumber")
	private String phone;

	public int getPermission() {
		return permission;
	}

	public void setPermission(int permission) {
		this.permission = permission;
	}

	public short getIsCurrentLoginAccount() {
		return isCurrentLoginAccount;
	}

	public void setIsCurrentLoginAccount(short isCurrentLoginAccount) {
		this.isCurrentLoginAccount = isCurrentLoginAccount;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getIdCode() {
		return idCode;
	}

	public void setIdCode(String idCode) {
		this.idCode = idCode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return  email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getBrief() {
		return brief;
	}

	public void setBrief(String brief) {
		this.brief = brief;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Override
    //判断对象o是否是当前user对象，若是，返回true；若不是，返回false
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (!(o instanceof User))
			return false;

		User user = (User) o;

		return id == user.id;
	}


	public static class Builder {
		private User user;
		private Builder builder;

		public Builder() {
			builder = this;
			user = new User();
		}

		public Builder id(String id) {
			user.id = id;
			return builder;
		}

		public Builder nickName(String name) {
			user.nickName = name;
			return builder;
		}

		public Builder avatar(String avatar) {
			user.avatar = avatar;
			return builder;
		}

		public Builder phone(String phone) {
			user.phone = phone;
			return builder;
		}

		public Builder email(String email) {
			user.email = email;
			return builder;
		}

		public Builder brief(String brief) {
			user.brief = brief;
			return builder;
		}

		public Builder company(String company) {
			user.company = company;
			return builder;
		}

		public Builder title(String title) {
			user.title = title;
			return builder;
		}

		public Builder password(String password) {
			user.password = password;
			return builder;
		}

		public Builder idCode(String idCode) {
			user.idCode = idCode;
			return builder;
		}

		public Builder token(String token) {
			user.token = token;
			return builder;
		}

		public Builder isCurrentLoginAccount(short isCurrentLoginAccount) {
			user.isCurrentLoginAccount = isCurrentLoginAccount;
			return builder;
		}

		public Builder permission(int permission) {
			user.permission = permission;
			return builder;
		}

		public User create() {
			return user;
		}
	}

    //创建一个匿名用户作为默认设置
	public static User Null = new Builder().id("匿名用户").nickName("匿名用户").permission(0).create();

	@Override
    //以分组的形式描述当前存放在parcelable中的对象
	public int describeContents() {
		return 0;
	}

	@Override
    /**
     *将user对象的属性值写入parcel中
     * @param dest - 接收写入值的parcel对象
     * @param flags - 描述对象将如何写入parcel中的标识信息
     * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
     */
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(id);
		dest.writeString(avatar);
		dest.writeString(email);
		dest.writeString(createTime);
		dest.writeString(gender);
		dest.writeString(brief);
		dest.writeString(phone);
		dest.writeString(token);
		dest.writeString(company);
		dest.writeString(title);
		dest.writeString(department);
	}


	public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
		@Override
        /**
         * 从Parcel类source中创建一个User类的实例，该实例的数据已经事先通过writeToParcel方法写入
         * @param source - 读取对象数据的parcel对象
         * @return user - 返回一个创建好的User对象
         * @see android.os.Parcelable.Creator#createFromParcel(android.os.Parcel)
         */
		public User createFromParcel(Parcel source) {
			User user = new User();
			user.id = source.readString();
			user.avatar = source.readString();
			user.email = source.readString();
			user.createTime = source.readString();
			user.gender = source.readString();
			user.brief = source.readString();
			user.phone = source.readString();
			user.token = source.readString();
			user.company = source.readString();
			user.title = source.readString();
			user.department = source.readString();

			return user;
		}

		@Override
        /**
         * 创建一个User类的数组
         * @param size - 数组大小
         * @return user[] - 指定大小的User类数组
         * @see android.os.Parcelable.Creator#newArray(int)
         */
		public User[] newArray(int size) {
			return new User[0];
		}
	};
}
